﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Content;

namespace RPGX.Graphics
{
    public class HUDObject_Button : HUDObject
    {
        #region Const Properties

        public const int Margin = 10;

        #endregion

        #region Properties

        public override string Name
        {
            get
            {
                return "Button";
            }
        }

        public Texture2D TextureToRender
        {
            get { return _textureToRender; }
            set { _textureToRender = value; }
        }
        private Texture2D _textureToRender = null;

        public Texture2D Texture
        {
            get { return _texture; }
            set { _texture = value; }
        }
        private Texture2D _texture = null;

        public Texture2D MouseOverTexture
        {
            get { return _mouseOverTexture; }
            set { _mouseOverTexture = value; }
        }
        private Texture2D _mouseOverTexture = null;

        public Texture2D MouseDownTexture
        {
            get { return _mouseDownTexture; }
            set { _mouseDownTexture = value; }
        }
        private Texture2D _mouseDownTexture = null;

        public string TextKey
        {
            get { return _textKey; }
            set { _textKey = value; }
        }
        private string _textKey = String.Empty;

        #endregion

        #region Public Methods

        public override bool Render(GameTime time, SpriteBatch spriteBatch, Rectangle bounds)
        {
            if (this.Texture == null)
                return false;

            Vector2 textSize = this.HUD.Text.CurrentFont.MeasureString(this.HUD.Text.GetText(this.TextKey));

            Rectangle pos = new Rectangle();
            pos.X = bounds.X + (int)this.PositionRelativeToParent.X;
            pos.Y = bounds.Y + (int)this.PositionRelativeToParent.Y;

            // when sizing for text, we need to take into account the margin
            if (this.Height - (HUDObject_Button.Margin * 2) < textSize.Y)
                this.Height = (int)textSize.Y + (HUDObject_Button.Margin * 2);

            if (this.Width - (HUDObject_Button.Margin * 2) < textSize.X)
                this.Width = (int)textSize.X + (HUDObject_Button.Margin * 2);

            pos.Width = this.Width;
            pos.Height = this.Height;

            spriteBatch.Draw(this.TextureToRender, pos, Color.White);

            HUDObject_TextArea textArea = new HUDObject_TextArea();
            textArea.HUD = this.HUD;
            textArea.Key = this.TextKey;
            textArea.Width = pos.Width - HUDObject_Button.Margin;
            textArea.Height = pos.Height - HUDObject_Button.Margin;
            textArea.PositionRelativeToParent = new Vector2(HUDObject_Button.Margin, HUDObject_Button.Margin);

            textArea.Render(time, spriteBatch, pos);

            foreach (var c in this.ChildObjects)
            {
                c.Render(time, spriteBatch, pos);
            }

            return true;
        }

        public override bool FromXML(XElement element, HUD hud)
        {
            this.ChildObjects = hud.LoadChildElements(element, this);

            string texture = element.Attribute("texture").Value;
            this.Texture = hud.Graphics.Game.Content.Load<Texture2D>(texture);

            string mouseOverTexture = element.Attribute("mouseovertexture").Value;
            this.MouseOverTexture = hud.Graphics.Game.Content.Load<Texture2D>(mouseOverTexture);

            string mouseDownTexture = element.Attribute("mousedowntexture").Value;
            this.MouseDownTexture = hud.Graphics.Game.Content.Load<Texture2D>(mouseDownTexture);

            this.TextureToRender = this.Texture;

            this.TextKey = element.Attribute("textkey").Value;

            return true;
        }

        public override bool UnLoad()
        {
            this.Texture.Dispose();
            this.Texture = null;

            this.MouseOverTexture.Dispose();
            this.MouseOverTexture = null;

            this.MouseDownTexture.Dispose();
            this.MouseDownTexture = null;

            return true;
        }

        #region Events

        public override void OnMouseOver(HUDMouseOverEventArgs e)
        {
            base.OnMouseOver(e);

            this.TextureToRender = this.MouseOverTexture;
        }

        public override void OnMouseDown(HUDMouseDownEventArgs e)
        {
            base.OnMouseDown(e);

            this.TextureToRender = this.MouseDownTexture;
        }

        public override void OnMouseLeave(HUDMouseLeaveEventArgs e)
        {
            base.OnMouseLeave(e);

            this.TextureToRender = this.Texture;
        }

        #endregion

        #endregion
    }
}
